package android.medicalcloud.com.yiapp.yilian.logic;

import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.MapBuilder;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.model.BaseModeal;
import android.medicalcloud.com.yiapp.yilian.model.ImConfigModel;
import android.medicalcloud.com.yiapp.yilian.utils.net.ConnectTool;

import java.io.Serializable;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class UserController {
    static UserController instance;

    private  String loginKey="UserLocalKey";
    private  String TempleloginKey="TempleUserLocalKey";
    private  String UserAvar="UserAvar";
    private  String saveTempleUserName="saveTempleUserName";
    public static UserController getInstance() {
        if (instance == null) {
            instance = new UserController();
        }
        return instance;
    }
    /**
     * login
     */
    public void login(ViewNetCallBack viewNetCallBack, String userName,String pass) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.Login,
                    new MapBuilder<String, Object>()
                            .add("loginName",userName)
                            .add("userPwd",pass)
                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    /**
     * login
     */
    public void loginIm( ViewNetCallBack callBack) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.imSig,
                    new MapBuilder<String, Object>()
                            .getMap()
                    , callBack
                    , ImConfigModel.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    /**
     * login
     */
    public void findPasword(ViewNetCallBack viewNetCallBack, String userName,String mobile,String verifyCode,String pasw) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.findPassword,
                    new MapBuilder<String, Object>()
                            .add("userName",userName)
                            .add("mobile",mobile)
                            .add("verifyCode",verifyCode)
                            .add("newPas",pasw)
                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    /**
     * login
     */
    public void updatePasword(ViewNetCallBack viewNetCallBack, String oldPas,String newPas,String verifyCode) {
        try {
            ConnectTool.httpRequest(
                    HttpConfig.updatePas,
                    new MapBuilder<String, Object>()
                            .add("oldPas",oldPas)
                            .add("newPas",newPas)
                            .add("verifyCode",verifyCode)
                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    public void sendPinCode(ViewNetCallBack viewNetCallBack, String mobile) {


        try {
            ConnectTool.httpRequest(
                    HttpConfig.Login,
                    new MapBuilder<String, Object>()

                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    public boolean isLogin(){
        return !StringTools.isNullOrEmpty(userToken());
    }

    public String userToken(){
        String token = PreferceManager.getInsance().getValueBYkey(loginKey);
        return token;
    }
    public String userTempleToken(){
        String token = PreferceManager.getInsance().getValueBYkey(TempleloginKey);
        return token;
    }

    public String saveToken(String token){
         PreferceManager.getInsance().saveValueBYkey(loginKey,token);
         PreferceManager.getInsance().saveValueBYkey(TempleloginKey,token);
        return token;
    }
    public void saveImageAvar(String path){
        PreferceManager.getInsance().saveValueBYkey(UserAvar,path);


    }

    public void saveTempleUserName(String str){
        PreferceManager.getInsance().saveValueBYkey(saveTempleUserName,str);


    }

    public String getTempleUserName(){
       return PreferceManager.getInsance().getValueBYkey(saveTempleUserName);


    }
    public String getImageAvar(){
        return PreferceManager.getInsance().getValueBYkey(UserAvar);


    }


    public void cleanToken() {
        PreferceManager.getInsance().saveValueBYkey(loginKey, "");
        PreferceManager.getInsance().saveValueBYkey(TempleloginKey, "");
    }

    public void simpleInfor(ViewNetCallBack viewNetCallBack){
        try {
            ConnectTool.httpRequest(
                    HttpConfig.userSimpleInfor,
                    new MapBuilder<String, Object>()

                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }
    public void detailInfor(ViewNetCallBack viewNetCallBack){
        try {
            ConnectTool.httpRequest(
                    HttpConfig.userDetailInfor,
                    new MapBuilder<String, Object>()

                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    public void loginOut(){
        try {
            ConnectTool.httpRequest(
                    HttpConfig.loginOut,
                    new MapBuilder<String, Object>()

                            .getMap()
                    , new CommonRespones()
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }

    public void loginOut(ViewNetCallBack callBack){
        try {
            ConnectTool.httpRequest(
                    HttpConfig.loginOut,
                    new MapBuilder<String, Object>()

                            .getMap()
                    ,callBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }


    private class CommonRespones implements ViewNetCallBack{
        @Override
        public void onConnectStart(Object o) {

        }

        @Override
        public void onConnectEnd() {

        }

        @Override
        public void onFail(Exception e, Object o, String reslut) {

        }

        @Override
        public void onData(Serializable result, int flag, boolean fromNet, Object o) {

        }
    }

    public void updateInfor(ViewNetCallBack viewNetCallBack,String expert,String summy){
        try {
            ConnectTool.httpRequest(
                    HttpConfig.userUpdate,
                    new MapBuilder<String, Object>()
                            .add("expert",expert)
                            .add("summy",summy)

                            .getMap()
                    , viewNetCallBack
                    , BaseModeal.class);
        } catch (Exception e) {
            Logger.e(e.getLocalizedMessage(), e);
        }
    }
}
